package com.gmrz.uaf.remote.plugin;

import com.gmrz.service.challengestore.ChallengeStoreException;
import com.gmrz.service.challengestore.ChallengeStoreService;
import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.common.service.challengestore.UAFChallengeStoreServiceProvider;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uas.plugin.provider.ServiceProvider;
import com.gmrz.util.Convert;
import com.gmrz.util.UUIDGenerator;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by gongyuqiang on 2017/5/25.
 */
public class UASServiceProvider  implements ServiceProvider {

    @Override
    public Connection getUasConnection(boolean autoCommit) throws SQLException{
        return UAFDBConnectionMgr.getConnection(autoCommit);
    }

    @Override
    public Connection getUasLogConnection(boolean autoCommit) throws SQLException{
        return UAFDBConnectionMgr.getLogConnection(autoCommit);
    }

    @Override
    public String generateID() {
        return UUIDGenerator.generateID();
    }

    @Override
    public String toBase64String(byte[] data) {
        return Convert.toBase64String(data);
    }

    @Override
    public String getRedisValue(String key) {
        UAFChallengeStoreServiceProvider uafChallengeStoreServiceProvider = GuiceUtil.getProcessorInjector()
                .getInstance(UAFChallengeStoreServiceProvider.class);
        ChallengeStoreService challengeStoreService = uafChallengeStoreServiceProvider.get();
        try {
            return challengeStoreService.getRedisValue(key);
        } catch (ChallengeStoreException e) {
            new RuntimeException("getRedisValue error key:"+key);

        }
        return null;
    }

    @Override
    public void setRedisValue(String key, int seconds, String value) {
        UAFChallengeStoreServiceProvider uafChallengeStoreServiceProvider = GuiceUtil.getProcessorInjector()
                .getInstance(UAFChallengeStoreServiceProvider.class);
        ChallengeStoreService challengeStoreService = uafChallengeStoreServiceProvider.get();
        try {
            challengeStoreService.setRedisValue(key,seconds,value);
        } catch (ChallengeStoreException e) {
            new RuntimeException("setRedisValue error key:"+key+",seconds:"+seconds+",value:"+value);
        }
    }
    @Override
    public void setrangeRedisValue(String key, String value) {
        UAFChallengeStoreServiceProvider uafChallengeStoreServiceProvider = GuiceUtil.getProcessorInjector()
                .getInstance(UAFChallengeStoreServiceProvider.class);
        ChallengeStoreService challengeStoreService = uafChallengeStoreServiceProvider.get();
        try {
            challengeStoreService.setrangeRedisValue(key,value);
        } catch (ChallengeStoreException e) {
            new RuntimeException("setRedisValue error key:"+key+",value:"+value);
        }
    }

    @Override
    public void deleteRedisValue(String key) {
        UAFChallengeStoreServiceProvider uafChallengeStoreServiceProvider = GuiceUtil.getProcessorInjector()
                .getInstance(UAFChallengeStoreServiceProvider.class);
        ChallengeStoreService challengeStoreService = uafChallengeStoreServiceProvider.get();
        try {
            challengeStoreService.deleteRedisValue(key);
        } catch (ChallengeStoreException e) {
            new RuntimeException("deleteRedisValue error key:"+key);
        }
    }
}
